//
//  StatusesCellTopView.swift
//  SinaMicroBlog
//
//  Created by wxy on 15/9/7.
//  Copyright © 2015年 wxy. All rights reserved.
//

import UIKit
import SDWebImage

// 顶部视图
class StatusesCellTopView: UIView {
    
    var statusesViewModel: StatusesViewModel? {
        
        didSet {
            // TODO: 设置界面数据
            nameLabel.text = statusesViewModel?.statuses.user?.name
            // sd_setImageWithURL 是OC方法 参数可以传递nil 不用强行解包
            iconImageView.sd_setImageWithURL(statusesViewModel?.userIconUrl)
            // 会员
            memberImageView.image = statusesViewModel?.userMemberImage
            // vip
            vipImageView.image = statusesViewModel?.userVipImage
            
            // TODO: 后续处理
//            timeLabel.text = statusesViewModel?.statuses.created_at
//            sourceLabel.text = statusesViewModel?.statuses.source
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        
        backgroundColor = UIColor.whiteColor()
        
        // 1.添加控件
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        
        // 2.自动布局
        let offset = CGPoint(x: StatusesCellMargin, y: 0)
        iconImageView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: StatusesCellIconWidth, height: StatusesCellIconWidth), offset: CGPoint(x: StatusesCellMargin, y: StatusesCellMargin))
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconImageView, size: nil, offset: offset)
        vipImageView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconImageView, size: nil, offset: CGPoint(x: 8, y: 8))
        memberImageView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: nameLabel, size: nil, offset: offset)
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconImageView, size: nil, offset: offset)
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: timeLabel, size: nil, offset: offset)
        
    }
    
    // MARK: - 懒加载控件
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel:UILabel = UILabel(title: "姓名", color: UIColor.darkGrayColor(), fontSize: 14)
    private lazy var vipImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_grassroot"))
    private lazy var memberImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    private lazy var timeLabel:UILabel = UILabel(title: "刚刚", color: UIColor.orangeColor(), fontSize: 10)
    private lazy var sourceLabel:UILabel = UILabel(title: "来自 新浪微博", color: UIColor.darkGrayColor(), fontSize: 10)
    
}
